﻿ $(function(){
     //可选框自动赋值id
     $('#zibei_no').change(function(){
         var zibei = $('#zibei_no option:selected').text();
         var val = $('#zibei_no option:selected').val();
         if($.empty(val)) zibei = '';
         $('#zibei').val(zibei);
         if(!$.empty(val)){
             var json = {'i1':'zibeiAuto','zibei_no':val};
             Te.post('/TeClan/Node/ajax',json,function(data){
                 if(!$.empty(data)) $('#select_node').html(Base64.decode(data));
             });
         }
     });
     
     // 姓名-自动完成字辈选择/数据重复性检测
     $('#name').blur(function(){
         var name = $(this).val();         
         if($.empty(name)) return;
         var arr = name.split('');
         if(arr.length != 3) return;//仅仅检测三个字的姓名
         var zibei = arr[1];
         var isZibei = false;
         var opts = $('#zibei_no option');
         var value = '';
         for(var i=0;i<opts.length;i++){             
             var dom = $(opts[i]);// dom => jquery 对象
             if(dom.text() == zibei){
                 //value = dom.attr('value');$.log(value);
                 dom.attr('selected',true);//设置选定值
                 $('#zibei').val(zibei);
                 //return;
                 zibei = dom.attr('value');
                 isZibei = true;
                 break;
             }
         }
         var gen_no = $('#gen_no').val();
         if($.empty(gen_no)) return;
         var json = {'i1':'cn_nameburl','name':name,'gen_no':gen_no};
         if(isZibei) json['zibei_no'] = zibei;
         Te.post('/TeClan/Node/ajax',json,function(data){
             //var json = $.parseJSON(data);
             eval('var json = '+data+';');
             //$.log(json);
             if(!$.empty(json['name'])) $('#form_tip').html(Base64.decode(json['name']));
             if(!$.empty(json['sel'])) $('#select_node').html(Base64.decode(json['sel']));
         });

         /*
         for(var k in opts){
             //$.log(opts[k]+':'+typeof(opts[k])+'//'+k);

             
             var dom = $(opts[k]);// dom => jquery 对象
             if(dom.text() == zibei){
                 //value = dom.attr('value');$.log(value);
                 dom.attr('selected',true);//设置选定值
                 $('#zibei').val(zibei);
                 $.log(i);
                 return;
             }
             
            i++;
         }
         */
     });

     // 提交检测
     $('input[type="submit"]').click(function(){
        var data = $("#crt_node").serializeArray();
        //var arr ={"name":"姓名","zibei_no":"字辈","zibei":"字辈","desc":"简介"};
        var arr ={"name":"姓名","sex":"性别","desc":"简介"};
        if(Te.check2("crt_node",arr,"form_tip")) return false;
        return true;
     });

     // 名字搜索
     $('#search').blur(function(){
         var name = $(this).val();
         if($.empty(name)) return;
         var geno = $('#gen_no').val();
         if($.empty(name)) return;
         Te.post('/TeClan/Node/ajax',{'i1':'searchName','name':name,'gen_no':geno},function(data){
             if(!$.empty(data)){
                 $('#select_node').html(Base64.decode(data));
             }
         });
     });
 });
 var Te = $.TeClan;
 //分页
 Te.pageTo = function(no){
     var genno = $('#gen_no').val();
     if($.empty(genno)) return;
     var json = {'i1':'getNode','no':no,'genno':genno};
     Te.post('/TeClan/Node/ajax',json,function(data){
         $('#select_node').html(data);
     });
 }
// 帮助性父母id写入
 Te.pHelper = function(dom){
     var pers = $(dom).attr('pers');
     var desc = $(dom).text();
     if($.empty(pers)) return;
     var arr = pers.split('/');
     var sex = arr[1];
     if(sex == 'M'){//其父
         $('#father_helper').val(desc);
         $('#father').val(arr[0]);
     }else if(sex == 'F'){//其母
         $('#mother_helper').val(desc);
         $('#mother').val(arr[0]);
     }
 }